package zhzzofbl;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import zhzz.AllIllnessOfZH;
import zhzz.SingleDataofzhzz;
import bingliandshuju.SingleDataofAll;

import com.example.animaldoctor03.JBXQ;
import com.example.animaldoctor03.R;
import com.example.animaldoctor03.ZhengDuanFF;

import commonClass.ADAQueryClinicalSymptomsKeyByDiseaseIDAction;
import commonClass.FunctionDispatcher;
import commonClass.RemoteActionsName;
import commonClass.FunctionDispatcher.IRequestCompeletListener;
import Modle.disease.ClinicalSymptomsKey;
import Modle.disease.Symptoms;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class bi3ofzh extends Activity {

	
	private String nameofBL3 = "";
	private String GLofBL3 ;
	private TextView  textofBL3= null ;
	private ListView listviewofbingli3 = null;
	private Button buttonofbingli3toJBXQ = null , buttonofbingli3forallchoice = null  ;
	private Button  buttonofbingli3toagain = null , buttonofBL3F = null ;

	
	private List<Object> listofBL3getinformation = new ArrayList<Object>();
	private List<Object> listofbi3getbasic = new ArrayList<Object>();
	private String animalIDofBL3 = "";
	private ArrayList<Object>resultListofBL3 = null;
	
	private  void initElemofBL3 (){
		
		setContentView(R.layout.bingli3);
		buttonofBL3F = (Button)findViewById(R.id.buttonofBLF3);
		textofBL3 = (TextView)findViewById(R.id.textviewofbinliof3);
		listviewofbingli3 = (ListView)findViewById(R.id.listofbingli3);
		buttonofbingli3forallchoice = (Button)findViewById(R.id.buttonofbingli34);
		buttonofbingli3toJBXQ = (Button)findViewById(R.id.buttonofbingli31);
		buttonofbingli3toagain = (Button)findViewById(R.id.buttonofbingli32);
		
		buttonofbingli3toagain.setOnClickListener(new listenerofbingli3toagain());
		buttonofbingli3toJBXQ.setOnClickListener(new listenetofbingli3toJBXQ());
		
		buttonofBL3F.setOnClickListener(new listenerofBL3F());
		
	}
private void getInformationofBL3 (){
		
		SingleDataofzhzz singleDataofbi2 = SingleDataofzhzz.getInstance();
		listofbi3getbasic = singleDataofbi2.getListinformation();
		SingleDataofAll singleDataofBL3 = SingleDataofAll.getInstance();
		
		listofBL3getinformation = singleDataofBL3.getListinformation();
	}
	
private void getInformationfromupofBL3(){
	Intent intentofBL3 = getIntent();
	Bundle bundleofBL3 = intentofBL3.getExtras();
	ClinicalSymptomsKey CofBL3 = (ClinicalSymptomsKey) bundleofBL3.getSerializable("shuju");
	animalIDofBL3 = CofBL3.getDiseaseID();
	nameofBL3 = CofBL3.getDiseaseName();
	DecimalFormat format = new DecimalFormat("#.0000");
	
	GLofBL3 =format.format( CofBL3.getDiseaseProbability()) ;
	
	Message msgofBL3 = new Message();
	msgofBL3.what = 1;
	handleofBL3.sendMessage(msgofBL3);
	
	
}
private ArrayList<Object> assemblyofBL3() {

	getInformationfromupofBL3();
	ArrayList<Object> list = new ArrayList<Object>();

	list.add(animalIDofBL3);
	// Integer i = new Integer(10);
	// list.add(i);

	return list;
}
private void bindingofBL3(final ArrayList<Object> list){
	try{
		
	getInformationofBL3();
	if(list.size() > 0){
		Log.v("22222", "bushikongzhi");
	}
	List<String> listofoldIdsofBL3 = new ArrayList<String>();
	for(int j = 0 ; j < listofbi3getbasic.size() ; j++){
		
		Symptoms ctextofBL3 = (Symptoms) listofbi3getbasic.get(j);
		listofoldIdsofBL3.add(ctextofBL3.getSymptomsID());
	}
	
	for(int i = 0 ; i < listofBL3getinformation.size(); i ++ ){
		
		Symptoms ctextofBL3 = (Symptoms) listofBL3getinformation.get(i);
		listofoldIdsofBL3.add(ctextofBL3.getSymptomsID());
		
	}
	
	List<Object> listofZZhuanofBL3 = new ArrayList<Object>();
	
	List<Object> listofBL3QC = new ArrayList<Object>();
	
	for(int j = 0 ; j < list.size() ; j ++){
		
		ClinicalSymptomsKey cetext1ofBL3 = (ClinicalSymptomsKey) list.get(j);
		String sofctext1ofBL3 = cetext1ofBL3.getClinicalSymptomsKeyID();
		for( String s : listofoldIdsofBL3){
			if(sofctext1ofBL3.equals(s)){
			
				listofBL3QC.add(cetext1ofBL3);
		}
		
	}
	}

		
	for(int i = 0 ; i < list.size(); i++ ){
		

		int jishuofBL3 = 0;
		ClinicalSymptomsKey ctext2ofBL3 = (ClinicalSymptomsKey) list.get(i);
		String sofctext2ofBL3 = ctext2ofBL3.getClinicalSymptomsKeyID();
		for(String s : listofoldIdsofBL3){
			if(!sofctext2ofBL3.equals(s)){
				jishuofBL3 ++;
				
			}
			if(jishuofBL3 == listofoldIdsofBL3.size()){
				listofZZhuanofBL3.add(ctext2ofBL3);
			}
			
		}
	}
	
		
		String[] resultofBL3 = new String[listofZZhuanofBL3.size()];
		// list.toArray(new String[list.size()]);
		for (int i = 0; i < listofZZhuanofBL3.size(); i++) {
			
			
			//haiyouwenti
			resultofBL3[i] =( (ClinicalSymptomsKey)listofZZhuanofBL3.get(i)).getClinicalSymptomsKey() ;
		}
		ArrayAdapter<String> arrayAdapterofBL3 = new  ArrayAdapter<>(this, android.R.layout.simple_list_item_multiple_choice ,resultofBL3 );
		listviewofbingli3.setAdapter(arrayAdapterofBL3);
		
		buttonofbingli3forallchoice.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 ArrayList<Object> listofnextofBL3 = new ArrayList <Object>();
				 List<String> listofb3ofid = new ArrayList<String>();
					for(long ids :listviewofbingli3.getCheckItemIds() ){
						
					int i = (int) ids;
						
						listofnextofBL3.add((ClinicalSymptomsKey)list.get(i));
						listofb3ofid.add(((ClinicalSymptomsKey)list.get(i)).getClinicalSymptomsKeyID());
					}
					
					//把数据储存好
					SingleDataofAll singleDataofAllofBL3 = SingleDataofAll.getInstance();
					singleDataofAllofBL3.setListinformation(listofnextofBL3);
					singleDataofAllofBL3.setidinformation(listofb3ofid);
					List<String> listofb3ofzh = new ArrayList<String>();
					for(int e = 0 ; e < listofnextofBL3.size() ; e++){
						listofb3ofzh.add(((ClinicalSymptomsKey)listofnextofBL3.get(e)).getClinicalSymptomsKey());
						
					}
					singleDataofAllofBL3.setstringinformatin(listofb3ofzh);
					try{
						Intent intentofbingli3toallchoice = new Intent (bi3ofzh.this ,Choiceofzh.class );
						startActivity(intentofbingli3toallchoice);
						finish();
						}catch(RuntimeException e){
							 android.app.AlertDialog.Builder builderofBL3oftoYXZZ = new AlertDialog.Builder(bi3ofzh.this);
							 builderofBL3oftoYXZZ.setMessage("不能使用该功能, 请重试或者使用其他功能");
							 builderofBL3oftoYXZZ.create().show();
						}
			}
		});
	}catch(NullPointerException e){
		android.app.AlertDialog.Builder builderofBL3 = new AlertDialog.Builder(bi3ofzh.this);
		builderofBL3.setMessage("无数据, 请用其他功能");
		builderofBL3.create().show();
	}
	
	}
@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		
		initElemofBL3();
		ArrayList<Object> parasListofBL3 = assemblyofBL3();
		
		final ArrayList<Object> resultListTofBL3 = null;
		
		ADAQueryClinicalSymptomsKeyByDiseaseIDAction iActionofBL3 = new ADAQueryClinicalSymptomsKeyByDiseaseIDAction();
		FunctionDispatcher functionDispacherofBL3 = new FunctionDispatcher();
		
		functionDispacherofBL3.exceute(RemoteActionsName.QueryClinicalSymptomsKeyByDiseaseIDAction, parasListofBL3, iActionofBL3, new IRequestCompeletListener() {

			@Override
			public void doCompelet(ArrayList<Object> resultList) {
				// TODO Auto-generated method stub
				try{
				Log.v("ZhengZhuangFZ2", "post:" + resultList.toString());
				bi3ofzh.this.resultListofBL3 = resultList;
				bindingofBL3(resultListofBL3);
				}catch(NullPointerException e){
					android.app.AlertDialog.Builder builderofBL3off = new AlertDialog.Builder(bi3ofzh.this);
					builderofBL3off.setMessage("无返回值 , 请用其他功能");
					builderofBL3off.create().show();
				}
			}
		},this);
	
	}



			class listenerofBL3F implements OnClickListener{

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					try{
					Intent intentofBL3F = new Intent(bi3ofzh.this , ZhengDuanFF.class);
					startActivity(intentofBL3F);
					finish();
					}catch(RuntimeException e){
						 android.app.AlertDialog.Builder builderofBL3ofF = new AlertDialog.Builder(bi3ofzh.this);
						builderofBL3ofF.setMessage("不能使用该功能, 请重试或者使用其他功能");
						builderofBL3ofF.create().show();
					}
					
				}
				
			}

	class listenerofbingli3toagain implements   OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try{

			Intent intentofbingli3toagain = new Intent (bi3ofzh.this , AllIllnessOfZH.class);
			/*intentofbingli3toagain.putExtras(bundleofagainofBL3);*/
			startActivity(intentofbingli3toagain);
			finish();
			}catch(RuntimeException e){
				 android.app.AlertDialog.Builder builderofBL3oftoA = new AlertDialog.Builder(bi3ofzh.this);
				builderofBL3oftoA.setMessage("不能使用该功能, 请重试或者使用其他功能");
				builderofBL3oftoA.create().show();
			}
		}

	}
	
	class listenetofbingli3toJBXQ implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
	try{
			Bundle bundleofBL3 = new Bundle();
			bundleofBL3.putString("shuju", animalIDofBL3);
			Intent intentofbingli3toJBXQ = new Intent (bi3ofzh.this , JBXQ.class);
			intentofbingli3toJBXQ.putExtras(bundleofBL3);
			startActivity(intentofbingli3toJBXQ);
			finish();
	}catch(RuntimeException e){
		android.app.AlertDialog.Builder builderofBL3oftoJBXQ= new AlertDialog.Builder(bi3ofzh.this);
		builderofBL3oftoJBXQ.setMessage("不能使用该功能, 请重试或者使用其他功能");
		builderofBL3oftoJBXQ.create().show();
	}
		}
		
	}
	public Handler handleofBL3 = new Handler() {
		
		public void  handleMessage(Message msg) {
			
			if(msg.what == 1){
				textofBL3.setText("病名：" + nameofBL3 + " " + "概率："+ "0" + GLofBL3 );
			}
		}
	};
}

